<template>
	<view class="login">
		<view class="login-body">
			<view class="info">
				<view class="title">会员登录</view>
				<view class="form-rows">
					<view class="form-row">
						<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/login/account.png" mode="widthFix" class="form-row-icon"></image>
						<input type="text" placeholder="请输入账号" placeholder-class="input-placeholder"
							class="form-row-input" v-model="account" />
					</view>
					<view class="form-row">
						<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/login/mima.png" mode="widthFix" class="form-row-icon"></image>
						<input type="password" placeholder="请输入密码" placeholder-class="input-placeholder"
							class="form-row-input" v-model="password" />
					</view>
					<view class="form-bot">
						<view class="form-bot-item" @click="navigateTo('/pages/auth/register')">注册账号</view>
						<view class="form-bot-item" @click="navigateTo('/pages/auth/resetpw')">忘记密码</view>
					</view>
				</view>

				<view class="form-btns">
					<button class="login-btn" :disabled="btnDisabled" @click="do_login()">登录</button>
					<!-- #ifdef APP-PLUS -->
					<view class="wx-login" @click="weixin_login">
						<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/login/wx-login.png" mode="widthFix"></image>
						<text>微信登录</text>
					</view>
					<!-- #endif -->
				</view>

			</view>
		</view>
	</view>
</template>

<script>
import {getCode} from "../../common/helper";

var validate = require("@/components/validate/validate.js");
	var _this;
	export default {
		data() {
			return {
				//账号
				account: '',
				//密码
				password: '',
				tips: '',
				btnDisabled: false,
				//手机号
				phoneas: '',
				//验证码
				code: '',
				input: [{
					type: 'text',
					pla: '请输入111'
				}]
			}
		},
		onLoad() {
			_this = this
			if (this.user_id > 0) {
				this.$u.route({
					url: "pages/index/index",
					type: "redirect"
				})
				return
			}
		},
		methods: {
			//微信登录
			weixin_login() {
				// #ifndef H5
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						// 登录成功
						uni.getUserInfo({
							provider: 'weixin',
							success: function(info) {
								// 获取用户信息成功, info.authResult保存用户信息
								_this.weixin_dl(info.userInfo)
							}
						})
					},
					fail: function(err) {
						// 登录授权失败  
						// err.code是错误码
						console.log(err);
					}
				});
				// #endif
			},
			async weixin_dl(userInfo) {
				const data = await _this.$post('api/weixin_dl', userInfo)
				if (data.code == 200) {
					uni.$u.toast(data.msg)
					//存入用户信息
					this.$u.vuex('user_id', data.data.user_id)
					//存入token
					this.$u.vuex('user_token', data.data.token)
					setTimeout(() => {
						this.$u.route({
							url: "pages/index/index",
							type: "reLaunch"
						})
					}, 1000)
				} else if (data.code == -100) {
					uni.$u.toast(data.msg)
					setTimeout(() => {
						//跳转到绑定手机号界面
						uni.navigateTo({
							url: `/pages/auth/bind-mobile?tid=${data.data.tid}`
						})
					}, 2000)
				} else {
					uni.$u.toast(data.msg)
				}
			},
			//账号密码登录
			async do_login() {
				//单独验证使用
				// let check = validate.checkResult('mobile', this.mobile);
				// if (!check) {
				// 	uni.$u.toast("手机号码格式有误")
				// 	return
				// }
				let params = {
					'mobile': this.account,
					'password': this.password
				}
				var rule = [{
						// 字段名
						name: 'mobile',
						rules: ['require', 'mobile'],
						errorMsg: {
							require: '账号不能为空',
							mobile: '账号格式有误',
						}
					},
					{
						// 字段名
						name: 'password',
						rules: ['require', 'min:6'],
						errorMsg: {
							require: '密码不能为空',
							min: '密码最少6位'
						}
					}
				];
				// 是否全部通过，返回Boolean
				let checked = validate.check(params, rule);
				if (!checked) {
					// 获取错误信息
					let err = validate.getError();
					uni.$u.toast(err[0])
					return;
				}
				_this.btnDisabled = true
				// #ifdef MP
				params.mp_code = await getCode()
				// #endif
				console.info(params)
				const data = await _this.$post('api/do_login', params)
				if (data.code === 200) {
					//存入用户信息
					this.$u.vuex('user_id', data.data.user_id)
					//存入token
					this.$u.vuex('user_token', data.data.token)
					setTimeout(() => {
						this.$u.route({
							url: "pages/index/index",
							type: "reLaunch"
						})
					}, 1000)
					uni.$u.toast(data.msg)
				} else {
					uni.$u.toast(data.msg)
				}
				setTimeout(() => {
					_this.btnDisabled = false
				}, 2000)
			},
			//跳转回上一页
			centerto() {
				this.$u.route({
					type: "back"
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/static/css/login.scss";
</style>